/*******************************************************************************
New Unit Technologies, a division of DBI, LLC.

Created for CS450 at West Virginia University, Fall 2003

Developers:	Matthew Bradley
		Andrew Lowery
		Richard Paletta
		J. Ryan Sigler

File name: comhan.c
*******************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "comhan.h"
#include "mpx_supt.h"
#include "pcb.h"

#define MPX_VERSION 4.2
#define MPX_MODULE 4
#define MPX_BUILD 95
#define MAX_FILE_NAME_SIZE 10

//initialize queues (temporary)
extern PCB   *running;
extern queue *READY_QUEUE;
extern queue *BLOCKED_QUEUE;
extern queue *SUS_BLOCKED_QUEUE;
extern queue *SUS_READY_QUEUE;

/*******************************************************************************
Procedure name: comhan()
Purpose: To accept commandline input and execute the
			appropriate procedure. Allows user to
			interface the MPX.
Authors Names: Andrew Lowery, Rich Paletta
Parameters: NONE
Returns: returns 1 when finished
Globals: commandline.name, char confirm_quit[]
Calls: sys_req(), unpackcommand(), quit(), help(),
		 list(), date(), version(), createpcb(), deletepcb(), block(),
		 unblock(), suspend(), resume(), setpriority(), showpcb(),
		 showall(), showready(), showblocked()

Change log: 9/6/03  - Drew writes initial code
				9/13/03 - Rich adds confirmation to exit
				9/17/03 - Rich adds comments
				9/18/03 - Rich adds error codes
				9/18/03 - Rich attempts to add history
				10/5/03 - Drew fixes compilation errors
                                10/7/03 - Rich, Ryan, Drew, and Matt debug
*******************************************************************************/
char p_msg[80];
int p_length;

int comhan(void)
{
	char command[80];
	int counter, i;
	PCB *pcb_n;

	strcpy(confirm_quit,"no");

	sprintf(p_msg,"----------------------------\n\0");
    p_length = strlen(p_msg);
    sys_req(WRITE, TERMINAL, p_msg, &p_length); 
    sprintf(p_msg,"What do you want to do today\n\0");
    p_length = strlen(p_msg);
    sys_req(WRITE, TERMINAL, p_msg, &p_length);                 //print header
	sprintf(p_msg,"----------------------------\n\0");
    p_length = strlen(p_msg);
    sys_req(WRITE, TERMINAL, p_msg, &p_length);
	sprintf(p_msg,"Enter command\n==>> \0");
    p_length = strlen(p_msg);
    sys_req(WRITE, TERMINAL, p_msg, &p_length);

	counter=600;
	sys_req(READ,TERMINAL,command,&counter);                   //get commandline

	//put_in_hist(command);

	unpackcommand(command);                                    //parse command

	do
	{      
		for (i=0;i<NUMCOMMANDS;i++)                        //check to see if command given
		{                                                  //is a valid command
			if (strcmp(name[i],commandline.name) == 0)
				break;
		}

		if (i<NUMCOMMANDS){
            //printf("about to call...\n");
			(*fp[i]) ();                    //call procedure
        }

		else
			printf("ERROR 01: Invalid command. Please enter a valid command.\n");

		if(strncmp(confirm_quit,"yes", strlen(confirm_quit)-1))
		{
																	 //if not quitting
            sprintf(p_msg,"Enter command\n==> \0");      //get next command
            p_length = strlen(p_msg);
	        sys_req(WRITE, TERMINAL, p_msg, &p_length);


			counter=79;
			sys_req(READ,TERMINAL,command,&counter);
				 //	put_in_hist(command);
			unpackcommand(command);
		}

	} while(strncmp(confirm_quit, "yes", strlen(confirm_quit)-1));   //if quit has been
																						  //confirmed,
	//printf("\nExiting CommandHandler\n");                            
    sprintf(p_msg,"Exiting CommandHandler\n \0");                          //exit comhan
    p_length = strlen(p_msg);
	sys_req(WRITE, TERMINAL, p_msg, &p_length);
    
    //Kill IDLE process
    pcb_n=findpcb("IDLE");
    removepcb(pcb_n, READY_QUEUE);
    killPCB(pcb_n);
    sys_req(EXIT, TERMINAL, NULL, NULL);
    
	return 1;
}



/*******************************************************************************
Procedure name: unpackcommand()
Purpose: Parses the command entered by the user at the
			commandline into the command and the arguments
Authors Names: Andrew Lowery, Matthew Bradley
Parameters: command[]
Returns: returns 1 when finished
Globals: commandline.name, commandline.args, commandline.arg[]
Calls: N/A

Change log: 9/6/03  - Drew writes initial code
                9/7/03 - Matt finishes code
        	9/17/03 - Rich adds comments
		10/5/03 - Drew and Rich fix commandline
			 argument overflow error that crashes
			 Turbo C 2.0 (we think)
		10/5/03 - Drew fixes compilation warnings
*******************************************************************************/

int unpackcommand(char command[])
{
	int pos,i;
	commandline.args=0;
	pos=0;

	if((strlen(command)-1)==0)
	{
		strcpy(command,"no");
	}

	while (command[pos]==' ')                                //parse out leading spaces
		pos++;

	i=0;

	while (command[pos] != ' ' && command[pos] != '\0' && command[pos] != '\n' && i < MAX_CMD_LEN)
	{
		commandline.name[i] = tolower(command[pos]);          //change command to lower case
		i++;                                                  //and set commandline.name
		pos++;
	}

	commandline.name[i] = '\0';                              //add terminator

	while (command[pos] != '\0' && command[pos] != '\n' && commandline.args<=MAX_NUM_ARGS)
	{
		while (command[pos] == ' ') pos++;		               //parse out spaces

		if(i>=MAX_ARG_LEN)
		{
			while(command[pos]!=' ')pos++;
			while(command[pos]==' ')pos++;
		}

		i=0;

		while(command[pos]!=' ' && command[pos]!='\0' && command[pos] != '\n' && i<MAX_ARG_LEN)
		//while(command[pos]!=' ' && command[pos]!='\0' && command[pos] != '\n')
		{
			commandline.arg[commandline.args][i]=tolower(command[pos]);
			i++;                                               //change arguments to lower case
			pos++;                                             //and set commandline.arg[]
		}

		if(i>0)
		{
			commandline.arg[commandline.args][i]='\0';         //add terminator to
			commandline.args++;                                //commandline.arg[]
		}

	}

	return 1;
}



/*******************************************************************************
Procedure name: quit()
Purpose: To allow the user to exit the system.
Authors Names: Rich Paletta
Parameters: NONE
Returns: returns 1 when finished
Globals: char confirm_quit[]
Calls: sys_req()

Change log: 9/13/03 - Rich creates procedure
				9/17/03 - Rich adds comments
				9/18/03 - Rich adds error codes
*******************************************************************************/



int quit(void)
{
	if(commandline.args>0)                                      //checks for valid
	{
		printf("ERROR 02: No arguments are necessary\n");        //number of args
	}

	else
	{
		int count=10;
		//printf("Do you want to quit?\n==> ");
        sprintf(p_msg,"Do you want to quit?\n==> \0");
        p_length = strlen(p_msg);
	    sys_req(WRITE, TERMINAL, p_msg, &p_length);                     //confirms quit
		sys_req(READ, TERMINAL, confirm_quit, &count);
	

		if((strlen(confirm_quit)-1)==0)
		{
			strcpy(confirm_quit,"no");

		}

        }
  	return 1;
}

/*******************************************************************************
Procedure name: date()
Purpose: Display and set date
Authors Names: Andrew Lowery, Matthew Bradley, Rich Paletta
Parameters: NONE
Returns: returns 1 when finished
Globals: commandline.args, commandline.arg[],
Calls: sys_set_date(), sys_get_date()

Change log: 9/13/03 - Drew begins code
				9/13/03 - Matt adds date validity checks
				9/17/03 - Rich adds comments
				9/18/03 - Ryan adds error codes
				10/5/03 - Drew fixes compilation warnings
*******************************************************************************/

int date(void)
{
	int settoMonth=0,settoDay=0,settoYear=0,leap=0,sentinel=0;
	date_rec systemDate;

	if (commandline.args == 0)                   // print current date
	{
		sys_get_date(&systemDate);
		//printf("Current System Date: %d/%d/%d\n\n", systemDate.month, systemDate.day,
		//systemDate.year);
		sprintf(p_msg,"Current System Date: %d/%d/%d\n\n\0", systemDate.month, systemDate.day,
		systemDate.year);
        p_length = strlen(p_msg);
	    sys_req(WRITE, TERMINAL, p_msg, &p_length);
		sentinel=0;
	}

	else
	{                                       //attempt to set date
		if ((commandline.args==1)||(commandline.args==3)||(commandline.args>4))
		{
			printf("ERROR 03: Invalid number of Arguments\nType `Help Date' for more information\n");             //check for valid number of args
		}

		else
		{
			if(commandline.args==2)
			{
				printf("ERROR 13: Invalid date format\nType `help date' for correct format\n\n");
			}

			else
			{
				if((commandline.args>0)&&(strcmp(commandline.arg[0],"set")!=0))
				{
					printf("ERROR 04: Invalid argument\n");                        //check for valid set argument
					sentinel=1;
				}

				if (strcmp(commandline.arg[0],"set")==0)
				{
					sys_get_date(&systemDate);
					settoMonth=atoi(commandline.arg[1]);      //set temp variables for date
					settoDay=atoi(commandline.arg[2]);        //to argument values
					settoYear=atoi(commandline.arg[3]);


					if((settoYear<1980)||(settoYear>9999))    //check for valid year
					{
						printf("ERROR 05:System only recognizes date between the years 1980 and 9999 AD\n");
						sentinel=1;
					}

					if((settoYear%400!=0)||((settoYear%4!=0)&&(settoYear%100==0)))
					{
						leap=0;              //check for non leap year

						if(((settoMonth==2)&&(leap==0))&&(settoDay>28)||(settoDay<1))
						{
							printf("\nERROR 06: Invalid day\n");       //check for Feb 29 of
							sentinel=1;
						}                     //non leap year

					}

					else
					{
						leap=1;
						if(((settoMonth==2)&&(leap==1))&&(settoDay>29)||(settoDay<1))
						{
							printf("\nERROR 06: Invalid day\n");
							sentinel=1;
						}
					}                                         //check for invalid day
																			//in Feb of leap year


					if(((settoMonth==9)||(settoMonth==4)||(settoMonth==6)||(settoMonth==11))&&((settoDay>30)||(settoDay<1)))
					{
						printf("\nERROR 06: Invalid day\n");        //check for invalid day in Sept, April, June, and November
						sentinel=1;
					}

					else
					{
						if((settoMonth>12)||(settoMonth<1))
						{
							printf("\nERROR 07: Invalid month\n");           //check for valid month
							sentinel=1;
						}


						else
						{
							if( (settoMonth != 9)||(settoMonth != 4)||(settoMonth != 6)||(settoMonth !=11) )
							{
								if( (settoDay > 31)||(settoDay < 1) )        //check for valid month in all
								{                    								//other months
									printf("\nERROR 06: Invalid day\n");
									sentinel=1;
								}
							}
						}
					}

					if(sentinel==0)
					{
						systemDate.month=settoMonth;               //If date is valid set system date
						systemDate.day=settoDay;
						systemDate.year=settoYear;
						sys_set_date(&systemDate);
						printf("\n");
					}
				}
			}
		}                                             //If date is invalid set arguments

		if((sentinel!=0))                               //to valid date to irradicate
		{                                               //erroneous error messages
			strcpy(commandline.arg[1],"1");
			strcpy(commandline.arg[2],"1");
			strcpy(commandline.arg[3],"2001");
		}
	}

	return 1;
}

/*******************************************************************************
Procedure name: version()
Purpose: Display the version of the MPX
Authors Names: Andrew Lowery
Parameters: NONE
Returns: returns 1 when finished
Globals: NONE
Calls: NONE

Change log: 9/13/03 - Drew creates procedure
				9/17/03 - Rich adds comments
				9/18/03 - Rich adds error codes
*******************************************************************************/


int version(void)
{
	if(commandline.args>0)
	{
		printf("ERROR 02: No arguments are necessary\n");    //check that there are no arguments
	}

	else
	{
		float version = MPX_VERSION;    //initialize version information
		int module = MPX_MODULE;
		int build = MPX_BUILD;                    //print out version information

		printf("\nNew Unit Technology's MultiProgramming eXecutive\n");
		printf("Version: %1.2lf - Module: R%i - Build: %i\n\n",version,module,build);
	}

	return 1;
}


/*******************************************************************************
Procedure name: help()
Purpose: To provide help to the user on how to use the MPX
Authors Names: Jimmy Sigler
Parameters: NONE
Returns: returns 1 when finished
Globals: commandline.arg[]
Calls: NONE

Change log: 9/13/03 - Ryan creates the procedure
	    9/14/03 - Ryan debugs error messages
	    9/17/03 - Rich adds comments
	    9/18/03 - Ryan adds error codes 
	    10/5/03 - Drew fixes compilation warning
            10/6/03 - Ryan deletes uneeded help files
	    10/6/03 - Ryan increases buffer size to take out the trash
	    10/6/03 - Matt adds new help files for R2
*******************************************************************************/


int help(void)
{
	int i;
	FILE *noarg;
	char line[700];

	strncpy(line, " ", 699);

	if(commandline.args>1)
	{
		printf("ERROR 08: Invalid number of arguments\n");     //check for valid number of arguments
	}

	else
	{
		if(commandline.args==0)
		{
			noarg = fopen("help.hb", "r");            	//if no arguments, open help.hb help file

			if(!noarg)
			{
				printf("\n\nERROR 09: Couldn't open help file\n\n");
				return 1;
			}

			fread(line,sizeof(char),700, noarg);   		//reads help file into buffer
			printf("%s\n\n",line);                 		//prints buffer to terminal

			strncpy(line," ",699);                 		//clears the buffer
			printf("\n\n");

			fclose(noarg);                         		//closes help.hb file
		}

		else
		{
			if(strcmp(commandline.arg[0],"help")==0)       //check to see if arg is "help"
			{
				noarg = fopen("help.hb", "r");         //open help.hb

				if(!noarg)
				{
					printf("\n\nERROR 09: Couldn't open help file\n\n");
					return 1;
				}

				fread(line,sizeof(char),700, noarg);      //set and print buffer
				printf("%s\n\n",line);

				strncpy(line," ",699);
				printf("\n\n");                           //clear buffer

				fclose(noarg);										//close help.hb file
			}

			if(strcmp(commandline.arg[0],"date")==0)         //check to see if arg is "date"
			{
				noarg = fopen("date.hb", "r");           //open date.hb

				if(!noarg)
				{
					printf("\n\nERROR 09: Couldn't open help file\n\n");
					return 1;
				}

				fread(line,sizeof(char),700, noarg);        //set and print buffer
				printf("%s\n\n",line);

				strncpy(line," ",699);
				printf("\n\n");                             //clear buffer

				fclose(noarg);
			}

			if(strcmp(commandline.arg[0],"spoon")==0)
			{
				printf("ERROR 666: There is no spoon.\n");
			}

			if(strcmp(commandline.arg[0],"version")==0)              //check to see if arg is "version"
			{
				noarg = fopen("version.hb", "r");                //open version.hb

				if(!noarg)
				{
					printf("\n\nERROR 09: Couldn't open help file\n\n");
					return 1;
				}

				fread(line,sizeof(char),700, noarg);
				printf("%s\n\n",line);                           //set and print buffer

				strncpy(line," ",699);
				printf("\n\n");
																					  //clear buffer
				fclose(noarg);                                   //close version.hb
			}

			if(strcmp(commandline.arg[0],"list")==0)                 //check to see if arg is "list"
			{
				noarg = fopen("list.hb", "r");                   //open list.hb

				if(!noarg)
				{
					printf("\n\nERROR 09: Couldn't open help file\n\n");
					return 1;
				}

				fread(line,sizeof(char),700, noarg);           //set and print buffer
				printf("%s\n\n",line);
				strncpy(line," ",699);                         //clear buffer
				printf("\n\n");

				fclose(noarg);
			}

  
			if(strcmp(commandline.arg[0],"quit")==0)          //check to see if arg is "quit"
			{
				noarg = fopen("quit.hb", "r");            //open quit.hb

				if(!noarg)
				{
					printf("\n\nERROR 09: Couldn't open help file\n\n");
					return 1;
				}

				fread(line,sizeof(char),700, noarg);      //set and print buffer
				printf("%s\n\n",line);

				strncpy(line," ",699);
				printf("\n\n");                           //clear buffer

				fclose(noarg);                            //close quit.hb
			}


			if(strcmp(commandline.arg[0],"createpcb")==0)          //check to see if arg is "createpcb"
			{
				noarg = fopen("create.hb", "r");            //open createpcb.hb
        
				if(!noarg)
				{
					printf("\n\nERROR 09: Couldn't open help file\n\n");
					return 1;
				}
                		
				fread(line,sizeof(char),700, noarg);      //set and print buffer
				printf("%s\n\n",line);
				strncpy(line," ",699);
				printf("\n\n");                           //clear buffer

				fclose(noarg);                            //close createpcb.hb
			}	

			if(strcmp(commandline.arg[0],"deletepcb")==0)          //check to see if arg is "deletepcb"
			{
				noarg = fopen("delete.hb", "r");            //open deletepcb.hb
                	
				if(!noarg)
				{
				printf("\n\nERROR 09: Couldn't open help file\n\n");
				return 1;
				}
                		
				fread(line,sizeof(char),700, noarg);      //set and print buffer
				printf("%s\n\n",line);
				strncpy(line," ",699);
				printf("\n\n");                           //clear buffer

				fclose(noarg);                            //close deletepcb.hb
			}

			if(strcmp(commandline.arg[0],"block")==0)          //check to see if arg is "block"
			{
				noarg = fopen("block.hb", "r");            //open block.hb
        
				if(!noarg)
				{
					printf("\n\nERROR 09: Couldn't open help file\n\n");
					return 1;
				}
                		
				fread(line,sizeof(char),700, noarg);      //set and print buffer
				printf("%s\n\n",line);
				strncpy(line," ",699);
				printf("\n\n");                           //clear buffer

				fclose(noarg);                            //close block.hb
			}

			if(strcmp(commandline.arg[0],"unblock")==0)          //check to see if arg is "unblock"
			{
				noarg = fopen("unblock.hb", "r");            //open unblock.hb
        
				if(!noarg)
				{
					printf("\n\nERROR 09: Couldn't open help file\n\n");
					return 1;
				}
                		
				fread(line,sizeof(char),700, noarg);      //set and print buffer
				printf("%s\n\n",line);
				strncpy(line," ",699);
				printf("\n\n");                           //clear buffer
                	
				fclose(noarg);                            //close unblock.hb
			}


      			if(strcmp(commandline.arg[0],"suspend")==0)          //check to see if arg is "suspend"
			{
				noarg = fopen("suspend.hb", "r");            //open suspend.hb
        
				if(!noarg)
				{
					printf("\n\nERROR 09: Couldn't open help file\n\n");
					return 1;
				}
                		
				fread(line,sizeof(char),700, noarg);      //set and print buffer
				printf("%s\n\n",line);
				strncpy(line," ",699);
				printf("\n\n");                           //clear buffer

				fclose(noarg);                            //close suspend.hb
			}

			if(strcmp(commandline.arg[0],"resume")==0)          //check to see if arg is "resume"
			{
				noarg = fopen("resume.hb", "r");            //open resume.hb
        
				if(!noarg)
				{
					printf("\n\nERROR 09: Couldn't open help file\n\n");
					return 1;
				}
                		
				fread(line,sizeof(char),700, noarg);      //set and print buffer
				printf("%s\n\n",line);
				strncpy(line," ",699);
				printf("\n\n");                           //clear buffer

				fclose(noarg);                            //close resume.hb
			}

    			if(strcmp(commandline.arg[0],"setpriority")==0)          //check to see if arg is "setpriority"
			{
				noarg = fopen("priority.hb", "r");            //open setpriority.hb
        
				if(!noarg)
				{
					printf("\n\nERROR 09: Couldn't open help file\n\n");
					return 1;
				}
                		
				fread(line,sizeof(char),700, noarg);      //set and print buffer
				printf("%s\n\n",line);
				strncpy(line," ",699);
				printf("\n\n");                           //clear buffer

				fclose(noarg);                            //close setpriority.hb
			}

			if(strcmp(commandline.arg[0],"showpcb")==0)          //check to see if arg is "showpcb"
			{
				noarg = fopen("showpcb.hb", "r");            //open showpcb.hb
        
				if(!noarg)
				{
					printf("\n\nERROR 09: Couldn't open help file\n\n");
					return 1;
				}
                		
				fread(line,sizeof(char),700, noarg);      //set and print buffer
				printf("%s\n\n",line);
				strncpy(line," ",699);
				printf("\n\n");                           //clear buffer

				fclose(noarg);                            //close showpcb.hb
			}

			if(strcmp(commandline.arg[0],"showall")==0)          //check to see if arg is "showall"
			{
				noarg = fopen("showall.hb", "r");            //open showall.hb
        
				if(!noarg)
				{
					printf("\n\nERROR 09: Couldn't open help file\n\n");
					return 1;
				}
                		
				fread(line,sizeof(char),700, noarg);      //set and print buffer
				printf("%s\n\n",line);
				strncpy(line," ",699);
				printf("\n\n");                           //clear buffer

				fclose(noarg);                            //close showall.hb
			}

			if(strcmp(commandline.arg[0],"showready")==0)          //check to see if arg is "showready"
			{
				noarg = fopen("ready.hb", "r");            //open showready.hb
        
				if(!noarg)
				{
					printf("\n\nERROR 09: Couldn't open help file\n\n");
					return 1;
				}
                		
				fread(line,sizeof(char),700, noarg);      //set and print buffer
				printf("%s\n\n",line);
				strncpy(line," ",699);
				printf("\n\n");                           //clear buffer

				fclose(noarg);                            //close showready.hb
			}

			if(strcmp(commandline.arg[0],"showblocked")==0)          //check to see if arg is "showblocked"
			{
				noarg = fopen("blocked.hb", "r");            //open showblocked.hb
        
				if(!noarg)
				{
					printf("\n\nERROR 09: Couldn't open help file\n\n");
					return 1;
				}
                		
				fread(line,sizeof(char),700, noarg);      //set and print buffer
				printf("%s\n\n",line);
				strncpy(line," ",699);
				printf("\n\n");                           //clear buffer

				fclose(noarg);                            //close showblocked.hb
			}	       


			for (i=0;i<NUMCOMMANDS;i++)
			{
				if (strcmp(name[i],commandline.arg[0]) == 0)      //check to see if arg
				{
					break;
				}                                            //is a valid command
			}

			if (i==NUMCOMMANDS)
			{
				printf("\nERROR 10: %s is not a valid argument\n\n", commandline.arg);  //if not throw error
			}

			strcpy(commandline.arg[0],"");
		}
	}

	return 1;
}

/*******************************************************************************
Procedure name: list()
Purpose: To display all .mpx files in the current or
			specified directory.
Authors Names: Rich Paletta
Parameters: NONE
Returns: returns 1 when finished
Globals: commandline.args, commandline.arg[]
Calls: sys_open_dir(), sys_close_dir(), sys_get_entry()

Change log: 9/16/03 - Rich creates list procedure
				9/17/03 - Rich debugs & adds comments
				9/18/03 - Rich adds error codes
				10/5/03 - Drew fixes compilation warning
*******************************************************************************/

int list(void)
{
	char filename[MAX_FILE_NAME_SIZE],dir[MAX_FILE_NAME_SIZE],temp[MAX_FILE_NAME_SIZE];
	int errorcode,num_files=0,c_size=1;
	long file_size=0,t_size=0;

	if(commandline.args>1)
	{
		printf("ERROR 08: Invalid number of arguments\n");         //check for valid number
	}                                                        //of arguments

	else
	{
		if(commandline.args==0)
		{
			strcpy(dir,".");                                 //if no args, use current directory
		}

		else
		{
			strncpy(temp," ",MAX_FILE_NAME_SIZE);

			if(commandline.arg[0][0]=='\\')
			{

				while(c_size<strlen(commandline.arg[0]))
				{
					temp[c_size-1]=commandline.arg[0][c_size];
					c_size++;
				}
			}

			else
			{
				strcpy(temp,commandline.arg[0]);
			}

			strcpy(dir,temp);                                 //set dir to specified
		}                                                //directory

		printf("dir= %s\n",dir);
		errorcode=sys_open_dir(dir);                            //open directory

		if(errorcode==ERR_SUP_INVDIR)
		{                                                       //
			printf("ERROR 11: Invalid directory\n");                          //
		}                                                       //errors when opening

		if(errorcode==ERR_SUP_DIROPN)                           //directory
		{                                                       //
			printf("ERROR 12: Error opening directory");                      //
		}

		if(errorcode==0)			//directory opened ok
		{
			printf("FILENAME      \t\t FILE SIZE\n\n");                  //print header
			errorcode=sys_get_entry(filename, MAX_FILE_NAME_SIZE,&file_size);

			while( errorcode==0)                                         //get next entry
			{	                                                           //while there is a
				printf("%s.mpx\t\t %i bytes\n",filename, file_size);               //next entry
				num_files++;                                                 //count num files
				t_size=t_size+file_size;                                     //count total size
				errorcode=sys_get_entry(filename, MAX_FILE_NAME_SIZE,&file_size);
			}

			printf("\n There are %i .mpx files with a total size of %i bytes\n",num_files,t_size);
		}                          //print out total number of .mpx files and their total size

		sys_close_dir();           //close the directory
	}

	return 1;
}


/*******************************************************************************
Procedure name: createpcb()
Purpose: Creates a new PCB
Authors Names: Rich Paletta, Ryan Sigler
Parameters: none
Returns: none
Globals: commandline.arg[], READY_QUEUE
Calls: findpcb, newPCB, setupPCB, pinsert

Change log: 9/30/03 - Rich creates function
            10/4/03 - Rich and Ryan fix priority bug
            10/5/03 - Rich and Drew try to debug function
*******************************************************************************/
void createpcb(void)
{
	int error=0;
	int priority=130;
	PCB *mypcb;
	PCB *duh;

	

	if(commandline.args!=3)
	{
		printf("ERROR 08: Invalid number of arguments.\n");
		error =1;
	}

	if(strlen(commandline.arg[0])>8)
	{
		printf("ERROR 15: Process name must be no more than 8 characters.\n");
		error=1;
	}

	if((strcmp(commandline.arg[1],"app")!=0)&&(strcmp(commandline.arg[1],"sys")!=0))
	{
		printf("ERROR 16: Process class must be either sys or app.\n");
		error =1;
	}

	priority=atoi(commandline.arg[2]);

	if((priority>127)||(priority<-128))
	{
		printf("ERROR 17: Process priority must be an integer between -128 and 127.\n");
		error =1;
	}

	duh=findpcb(commandline.arg[0]);

	if(duh!=NULL)                               //check to make sure pcb does not already exist
	{
		printf("ERROR 18: Process name already exists, please use a different name.\n");
		error=1;
	}

	if(error==0)
	{
		mypcb=newPCB();

                if(strcmp(commandline.arg[1],"app")==0)
		{
			setupPCB(mypcb,commandline.arg[0],APP,priority, NULL, NULL);
		}                                                                 //setup pcb

		else
		{
			setupPCB(mypcb,commandline.arg[0],SYS,priority, NULL, NULL);
		}

                printf("PCB %s was created.\n",mypcb->name);

		pinsert(mypcb,READY_QUEUE);                                //insert into READY_QUEUE
	}
}



/*******************************************************************************
Procedure name: deletepcb()
Purpose: deletes a new PCB
Authors Names: Rich Paletta, Ryan Sigler
Parameters: none
Returns: none
Globals: commandline.arg[], READY_QUEUE, SUS_READY_QUEUE, BLOCKED_QUEUE,
			SUS_BLOCKED_QUEUE READY, SUS_READY, BLOCKED, SUS_BLOCKED
Calls: findpcb, removepcb, freePCB

Change log: 9/30/03 - Rich creates function
            10/4/03 - Rich and Ryan fix priority bug
	    10/5/03 - Rich and Drew try to debug function
	    10/5/03 - Drew fixes pointer warnings
*******************************************************************************/

void deletepcb()
{
	PCB *todel;
	int error;
	char pcbtodel[9];
        char dummy[9];
	queue *delqueue=NULL;

	if(commandline.args!=1)
	{
		printf("ERROR 08: Invalid number of arguments.\n");
	}

	else
	{
		strcpy(pcbtodel,commandline.arg[0]);   //set pcbtodel
		todel=findpcb(pcbtodel);

		if(todel!=NULL)
		{

			if(todel->state==READY)
			{
				delqueue = READY_QUEUE;
			}

			if(todel->state==BLOCKED)
			{
				delqueue = BLOCKED_QUEUE;
			}
																	//set active queue
			if(todel->state==SUS_READY)
			{
				delqueue = SUS_READY_QUEUE;
			}

			if(todel->state==SUS_BLOCKED)
			{
				delqueue = SUS_BLOCKED_QUEUE;
			}
                        			                       
			removepcb(todel,delqueue);              //remove from queue
			strcpy(dummy,todel->name);                        
			error=sys_free_mem(todel);                //free memory

			if(error!=0)
			{
				printf("ERROR 19: Failed to delete PCB.\n");
			}
			else
			{
                        	printf("PCB %s was deleted.\n",dummy);
                        }
		}

		else
		{
			printf("ERROR 20: PCB does not exist\n");
		}
	}
}

/*******************************************************************************
Procedure name: block()
Purpose: Moves a PCB from the READY_QUEUE to the BLOCKED_QUEUE or from the
			SUS_READY_QUEUE to the SUS_BLOCKED_QUEUE
Authors Names: Rich Paletta, Ryan Sigler
Parameters: none
Returns: none
Globals: commandline.arg[], READY_QUEUE, SUS_READY_QUEUE, BLOCKED_QUEUE,
			SUS_BLOCKED_QUEUE READY, SUS_READY, BLOCKED, SUS_BLOCKED
Calls: findpcb, removepcb, insert

Change log: 9/30/03 - Rich creates function
            10/5/03 - Rich and Drew try to debug function
*******************************************************************************/

void block(void)
{
	PCB *pcb;
	queue *delqueue;

	if(commandline.args!=1)
	{
		printf("ERROR 08: Invalid number of arguments.\n");
	}

	else
	{
		pcb=findpcb(commandline.arg[0]);

		if(pcb==NULL)
		{
			printf("ERROR 20: PCB does not exist.\n");          //make sure pcb exists
		}

		else
		{
			if(pcb->state==READY)
			{
				delqueue = READY_QUEUE;
			}

			if(pcb->state==BLOCKED)
			{
				delqueue = BLOCKED_QUEUE;              //set active queue
			}

			if(pcb->state==SUS_READY)
			{
				delqueue = SUS_READY_QUEUE;
			}

			if(pcb->state==SUS_BLOCKED)
			{
				delqueue = SUS_BLOCKED_QUEUE;
			}

			if((pcb->state==BLOCKED) ||(pcb->state==SUS_BLOCKED))
			{
				printf("ERROR 22: PCB already Blocked.\n");
                        }
			else
                        {
				removepcb(pcb,delqueue);                     //remove from queue
			}

			if(pcb->state==READY)
			{
				insert(pcb,BLOCKED_QUEUE);                    //insert into queue
				pcb->state=BLOCKED;
				printf("PCB %s has been moved to the BLOCKED_QUEUE.\n",pcb->name);
			}

			if(pcb->state==SUS_READY)
			{
				insert(pcb,SUS_BLOCKED_QUEUE);
				pcb->state=SUS_BLOCKED;
				printf("PCB %s has been moved to the SUS_BLOCKED_QUEUE.\n",pcb->name);
			}
			
                        	
		}
	}
}

/*******************************************************************************
Procedure name: unblock()
Purpose: Moves a PCB to the READY_QUEUE from the BLOCKED_QUEUE or to the
			SUS_READY_QUEUE from the SUS_BLOCKED_QUEUE
Authors Names: Rich Paletta, Ryan Sigler
Parameters: none
Returns: none
Globals: commandline.arg[], READY_QUEUE, SUS_READY_QUEUE, BLOCKED_QUEUE,
			SUS_BLOCKED_QUEUE READY, SUS_READY, BLOCKED, SUS_BLOCKED
Calls: findpcb, removepcb, insert

Change log: 9/30/03 - Rich and Ryan create function
	    10/5/03 - Rich and Drew try to debug function
*******************************************************************************/


void unblock(void)
{
	PCB *pcb;
	queue *delqueue;

	if(commandline.args!=1)
	{
		printf("ERROR 08: Invalid number of arguments.\n");
	}

	else
	{
		pcb=findpcb(commandline.arg[0]);

		if(pcb==NULL)
		{                                                          //make sure pcb exists
			printf("ERROR 20: PCB does not exist.\n");
		}

		else
		{
			if(pcb->state==READY)
			{
				delqueue = READY_QUEUE;
			}

			if(pcb->state==BLOCKED)
			{
				delqueue = BLOCKED_QUEUE;
			}
																				//set active queue
			if(pcb->state==SUS_READY)
			{
				delqueue = SUS_READY_QUEUE;
			}

			if(pcb->state==SUS_BLOCKED)
			{
				delqueue = SUS_BLOCKED_QUEUE;
			}
			if((pcb->state==READY) ||(pcb->state==SUS_READY))
			{
				printf("ERROR 23: PCB is not Blocked.\n");
                        }
			else
                        {
				removepcb(pcb,delqueue);                     //remove from queue
			}

			if(pcb->state==BLOCKED)
			{
				pinsert(pcb,READY_QUEUE);                   //insert into pcb
				pcb->state=READY;
				printf("PCB %s has been moved to the READY_QUEUE.\n",pcb->name);
			}

			if(pcb->state==SUS_BLOCKED)
			{
				insert(pcb,SUS_READY_QUEUE);
				pcb->state=SUS_READY;
				printf("PCB %s has been moved to the SUS_READY_QUEUE.\n",pcb->name);
			}
		}
	}
}

/*******************************************************************************
Procedure name: suspend()
Purpose: Moves a PCB from the READY_QUEUE to the SUS_READY_QUEUE or from
			the BLCOKED_QUEUE to the SUS_BLOCKED_QUEUE
Authors Names: Rich Paletta, Ryan Sigler
Parameters: none
Returns: none
Globals: commandline.arg[], READY_QUEUE, SUS_READY_QUEUE, BLOCKED_QUEUE,
			SUS_BLOCKED_QUEUE READY, SUS_READY, BLOCKED, SUS_BLOCKED
Calls: findpcb, removepcb, insert

Change log: 9/30/03 - Rich creates function
	    10/5/03 - Rich and Drew try to debug function
*******************************************************************************/


void suspend(void)
{
	PCB *pcb;
	queue *delqueue;

	if(commandline.args!=1)
	{
		printf("ERROR 08: Invalid number of arguments.\n");
	}

	else
	{
		pcb=findpcb(commandline.arg[0]);

		if(pcb==NULL)
		{
			printf("ERROR 20: PCB does not exist.\n");      //make sure pcb exists
		}

		else
		{
			if(pcb->state==READY)
			{
				delqueue = READY_QUEUE;
			}

			if(pcb->state==BLOCKED)
			{
				delqueue = BLOCKED_QUEUE;
			}

			if(pcb->state==SUS_READY)
			{                                            //set active queue
				delqueue = SUS_READY_QUEUE;
			}

			if(pcb->state==SUS_BLOCKED)
			{
				delqueue = SUS_BLOCKED_QUEUE;
			}

			if((pcb->state==SUS_READY) ||(pcb->state==SUS_BLOCKED))
			{
				printf("ERROR 24: PCB already Suspended.\n");
                        }
			else
                        {
				removepcb(pcb,delqueue);                     //remove from queue
			}
			if(pcb->state==BLOCKED)
			{                                             //insert into queue
				insert(pcb,SUS_BLOCKED_QUEUE);
				pcb->state=SUS_BLOCKED;
				printf("PCB %s has been moved to the SUS_BLOCKED_QUEUE.\n",pcb->name);
			}

			if(pcb->state==READY)
			{
				insert(pcb,SUS_READY_QUEUE);
				pcb->state=SUS_READY;
				printf("PCB %s has been moved to the SUS_READY_QUEUE.\n",pcb->name);
			}
		}
	}
}


/*******************************************************************************
Procedure name: resume()
Purpose: Moves a PCB to the READY_QUEUE from the SUS_READY_QUEUE or to the
			BLCOKED_QUEUE from the SUS_BLOCKED_QUEUE
Authors Names: Rich Paletta, Ryan Sigler
Parameters: none
Returns: none
Globals: commandline.arg[], READY_QUEUE, SUS_READY_QUEUE, BLOCKED_QUEUE,
			SUS_BLOCKED_QUEUE READY, SUS_READY, BLOCKED, SUS_BLOCKED
Calls: findpcb, removepcb, insert

Change log: 10/4/03 - Rich creates function
            10/7/03 - Rich and Ryan fix double resume error
*******************************************************************************/

void resume(void)
{
	PCB *pcb;
	queue *delqueue;

	if(commandline.args!=1)
	{
		printf("ERROR 08: Invalid number of arguments.\n");
	}

	else
	{
		pcb=findpcb(commandline.arg[0]);

		if(pcb==NULL)
		{
			printf("ERROR 20: PCB does not exist.\n");            	//make sure pcb exists
		}

		else
		{

			if(pcb->state==READY)
			{
				delqueue = READY_QUEUE;
			}

			if(pcb->state==BLOCKED)
			{                                                 	//set active queue
				delqueue = BLOCKED_QUEUE;
			}

			if(pcb->state==SUS_READY)
			{
				delqueue = SUS_READY_QUEUE;
			}

			if(pcb->state==SUS_BLOCKED)
			{
				delqueue = SUS_BLOCKED_QUEUE;
			}

			if((pcb->state==READY) ||(pcb->state==BLOCKED))
			{
				printf("ERROR 25: PCB is not Suspended.\n");
                        }
			else
                        {
				removepcb(pcb,delqueue);                     	//remove from queue
			}
																			//remove from queue
			if(pcb->state==SUS_READY)
			{                                                       //insert pcb into new queue
				pinsert(pcb,READY_QUEUE);
				pcb->state=READY;
				printf("PCB %s has been moved to the READY_QUEUE.\n",pcb->name);
			}

			if(pcb->state==SUS_BLOCKED)
			{
				insert(pcb,BLOCKED_QUEUE);
				pcb->state=BLOCKED;
				printf("PCB %s has been moved to the BLOCKED_QUEUE.\n",pcb->name);
			}
		}
	}
}

/*******************************************************************************
Procedure name: setpriority()
Purpose: changes the priority of the pcb indicated in arg[0] to the
			value of arg[1]
Authors Names: Rich Paletta, Ryan Sigler
Parameters: none
Returns: none
Globals: commandline.arg[]
Calls: findpcb

Change log: 9/30/03 - Rich creates function
	    10/4/03 - Rich and Ryan fix priority bug
            10/7/03 - Rich and Ryan fix the setpriority error with READY QUEUE
*******************************************************************************/

void setpriority(void)
{
	PCB *pcb;
	int newpriority;

	if(commandline.args!=2)
	{
		printf("ERROR 08: Invalid number of arguments.\n");
	}

	else
	{
		newpriority=atoi(commandline.arg[1]);
		pcb=findpcb(commandline.arg[0]);

		if(pcb==NULL)
		{
			printf("ERROR 20: PCB does not exist.\n");        	 //make sure pcb exists
		}

		else
		{
			if((newpriority<-128)||(newpriority>127))              	//check for valid priority
			{
				printf("ERROR 17: Process priority must be an integer between -128 and 127.\n");
			}

			else
			{
				pcb->priority=newpriority;                    	//set priority

				printf("Priority of %s has been changed to %i.\n",pcb->name,pcb->priority);

				if(pcb->state==READY)
				{
					removepcb(pcb,READY_QUEUE);
					pinsert(pcb,READY_QUEUE);
                                }

			}
		}
	}
}


/*******************************************************************************
Procedure name: showpcb()
Purpose: Displays the value of all of a PCB's attributes
Authors Names: Rich Paletta, Ryan Sigler
Parameters: none
Returns: none
Globals: commandline.arg[]
Calls: findpcb

Change log: 9/30/03 - Rich and Ryan create function
            10/4/03 - Rich and Ryan fix function to correspond with queue.c
	    10/5/03 - Rich and Drew try to debug function
            10/7/03 - Rich beautifies the display of attributes
*******************************************************************************/


void showpcb(void)
{
	PCB *pcb;
	
	if(commandline.args!=1)
	{
		printf("ERROR 08: Invalid number of arguments.\n");
	}

	else
	{
		pcb=findpcb(commandline.arg[0]);               			//make sure pcb exists

		if(pcb==NULL)
		{
			printf("ERROR 20: PCB does not exist.\n");
		}

		else
		{      
			printf("***********************************\n");
			printf("Name:    \t\t%s\n",pcb->name);
			if(pcb->pclass==0)
                        {
				printf("Class:   \t\tApplication\n");
			}
			else
			{
				printf("Class:   \t\tSystem\n");
                        }
			printf("Priority:\t\t%i\n",pcb->priority);        	//print out attributes

			switch(pcb->state)
			{
			case 0:	 printf("State:   \t\tRUNNING\n");
                        	 break;
			case 1:  printf("State:   \t\tREADY\n");
                        	 break;
			case 2:  printf("State:   \t\tBLOCKED\n");
                        	 break;
			case 3:  printf("State:   \t\tSUS_READY\n");
                        	 break;
			case 4:  printf("State:   \t\tSUS_BLOCKED\n");
                        	 break;
			default: printf("State:   \t\tERROR!!\n");
                        }
			printf("Stack:   \t\t%i\n",pcb->stack);
			printf("Size:    \t\t%i\n",pcb->mem_size);
			printf("Load add:\t\t%i\n",pcb->load);
			printf("Exe add: \t\t%i\n",pcb->exe);
			printf("***********************************\n");
		}
	}
}



/*******************************************************************************
Procedure name: showall()
Purpose: Displays the value of all PCB's attributes
Authors Names: Rich Paletta, Ryan Sigler
Parameters: none
Returns: none
Globals: none
Calls: showready, showblocked

Change log: 9/30/03 - Rich and Ryan create function
            10/5/03 - Rich and Ryan fix function to correspond with queue.c
*******************************************************************************/

void showall(void)
{                                                                          	       
	if(commandline.args!=0)
	{
		printf("ERROR 08: Invalid number of arguments.\n");
	}

	else
	{
		showready();
		showblocked();
	}
}

/*******************************************************************************
Procedure name: showready()
Purpose: Displays the attributes of all PCB's in the READY_QUEUE
			and the SUS_READY_QUEUE
Authors Names: Rich Paletta, Ryan Sigler
Parameters: none
Returns: none
Globals: READY_QUEUE, SUS_READY_QUEUE
Calls: traversequeue

Change log: 9/30/03 - Rich and Ryan create function
            10/5/03 - Rich and Ryan fix function to correspond with queue.c
*******************************************************************************/

void showready(void)
{
	if(commandline.args!=0)
	{
		printf("ERROR 08: Invalid number of arguments.\n");
	}

	else
	{
		printf("\nContents of Ready Queue\n");           			//print READY_QUEUE
		traversequeue(READY_QUEUE);

		printf("\nContents of Suspended Ready Queue\n"); 			//print SUS_READY_QUEUE
		traversequeue(SUS_READY_QUEUE);
	}
}

/*******************************************************************************
Procedure name: showblocked()
Purpose: Displays the attributes of all PCB's in the BLOCKED_QUEUE
			and the SUS_BLOCKED_QUEUE
Authors Names: Rich Paletta, Ryan Sigler
Parameters: none
Returns: none
Globals: BLOCKED_QUEUE, SUS_BLOCKED_QUEUE
Calls: traversequeue

Change log: 9/30/03 - Rich and Ryan create function
            10/5/03 - Rich and Ryan fix function to correspond with queue.c
*******************************************************************************/

void showblocked(void)
{
	if(commandline.args!=0)
	{
		printf("ERROR 08: Invalid number of arguments.\n");
	}

	else
	{
		printf("\nContents of Blocked Queue\n");  				 //print BLOCKED_QUEUE
		traversequeue(BLOCKED_QUEUE);

		printf("\nContents of Suspended Blocked Queue\n");			 //print SUS_BLOCKED_QUEUE
		traversequeue(SUS_BLOCKED_QUEUE);
	}
}
/*******************************************************************************
Procedure name: loadp()
Purpose: Loads a specified PCB with specified priority into the Suspended
         Ready Queue to wait for execution.
Authors Names: Johnathon Trumble
Parameters: none
Returns: none
Globals: none
Calls: loadprocs

Change log: 10-30-08 - Johnathon wrote function.
*******************************************************************************/

void loadp(void){
     if(commandline.args!=2){
         printf("ERROR 08: Invalid number of arguments.\n");
     }
     else
         loadprocs(commandline.arg[0], commandline.arg[1]);
}

/*******************************************************************************
Procedure name: terminate()
Purpose: Terminates a specified PCB from the system.
Authors Names: Johnathon Trumble
Parameters: none
Returns: none
Globals: READY_QUEUE, BLOCKED_QUEUE, SUS_READY_QUEUE, SUS_BLOCKED_QUEUE
Calls: findpcb, removepcb, killPCB

Change log: 10-30-08 - Johnathon wrote function.
*******************************************************************************/

void terminate(void){
     PCB *pcb;
     if(commandline.args!=1){
         printf("ERROR 08: Invalid number of arguments.\n");
     }
     else{
         pcb=findpcb(commandline.arg[0]);
         switch((*pcb).state){
			case 1:  removepcb(pcb, READY_QUEUE);
                        	 break;
			case 2:  removepcb(pcb, BLOCKED_QUEUE);
                        	 break;
			case 3:  removepcb(pcb, SUS_READY_QUEUE);
                        	 break;
			case 4:  removepcb(pcb, SUS_BLOCKED_QUEUE);
                        	 break;
			default: printf("PCB does not have a queue?");
            }
         killPCB(pcb);
     }
}

/*******************************************************************************
Procedure name: blockPCB()
Purpose: Moves a PCB from the READY_QUEUE to the BLOCKED_QUEUE or from the
			SUS_READY_QUEUE to the SUS_BLOCKED_QUEUE
Authors Names: Rich Paletta, Ryan Sigler
Parameters: pcb
Returns: none
Globals: commandline.arg[], READY_QUEUE, SUS_READY_QUEUE, BLOCKED_QUEUE,
			SUS_BLOCKED_QUEUE READY, SUS_READY, BLOCKED, SUS_BLOCKED
Calls: insert

Change log: 11/17/08
*******************************************************************************/

void blockPCB(PCB *pcb)
{
	queue *delqueue;

		if(pcb==NULL)
		{
			printf("ERROR 20: PCB does not exist.\n");          //make sure pcb exists
		}

		else
		{
			if(pcb->state==READY)
			{
				delqueue = READY_QUEUE;
			}

			if(pcb->state==BLOCKED)
			{
				delqueue = BLOCKED_QUEUE;              //set active queue
			}

			if(pcb->state==SUS_READY)
			{
				delqueue = SUS_READY_QUEUE;
			}

			if(pcb->state==SUS_BLOCKED)
			{
				delqueue = SUS_BLOCKED_QUEUE;
			}

			if((pcb->state==BLOCKED) ||(pcb->state==SUS_BLOCKED))
			{
				printf("ERROR 22: PCB already Blocked.\n");
                        }
			else
                        {
				removepcb(pcb,delqueue);                     //remove from queue
			}

			if(pcb->state==READY)
			{
				insert(pcb,BLOCKED_QUEUE);                    //insert into queue
				pcb->state=BLOCKED;
				printf("PCB %s has been moved to the BLOCKED_QUEUE.\n",pcb->name);
			}

			if(pcb->state==SUS_READY)
			{
				insert(pcb,SUS_BLOCKED_QUEUE);
				pcb->state=SUS_BLOCKED;
				printf("PCB %s has been moved to the SUS_BLOCKED_QUEUE.\n",pcb->name);
			}
    	}
}

/*******************************************************************************
Procedure name: unblockPCB()
Purpose: Moves a PCB to the READY_QUEUE from the BLOCKED_QUEUE or to the
			SUS_READY_QUEUE from the SUS_BLOCKED_QUEUE
Authors Names: Rich Paletta, Ryan Sigler
Parameters: pcb
Returns: none
Globals: commandline.arg[], READY_QUEUE, SUS_READY_QUEUE, BLOCKED_QUEUE,
			SUS_BLOCKED_QUEUE READY, SUS_READY, BLOCKED, SUS_BLOCKED
Calls: pinsert, insert

Change log:
*******************************************************************************/


void unblockPCB(PCB *pcb)
{
	queue *delqueue;

		if(pcb==NULL)
		{                                                          //make sure pcb exists
			printf("ERROR 20: PCB does not exist.\n");
		}

		else
		{
			if(pcb->state==READY)
			{
				delqueue = READY_QUEUE;
			}

			if(pcb->state==BLOCKED)
			{
				delqueue = BLOCKED_QUEUE;
			}
																				//set active queue
			if(pcb->state==SUS_READY)
			{
				delqueue = SUS_READY_QUEUE;
			}

			if(pcb->state==SUS_BLOCKED)
			{
				delqueue = SUS_BLOCKED_QUEUE;
			}
			if((pcb->state==READY) ||(pcb->state==SUS_READY))
			{
				printf("ERROR 23: PCB is not Blocked.\n");
                        }
			else
                        {
				removepcb(pcb,delqueue);                     //remove from queue
			}

			if(pcb->state==BLOCKED)
			{
				pinsert(pcb,READY_QUEUE);                   //insert into pcb
				pcb->state=READY;
				printf("PCB %s has been moved to the READY_QUEUE.\n",pcb->name);
			}

			if(pcb->state==SUS_BLOCKED)
			{
				insert(pcb,SUS_READY_QUEUE);
				pcb->state=SUS_READY;
				printf("PCB %s has been moved to the SUS_READY_QUEUE.\n",pcb->name);
			}
		}
}

/*******************************************************************************
Procedure name: history()
Purpose: To allow the user view a list of previously entered
			commands and re-execute them
Authors Names: Rich Paletta
Parameters: NONE
Returns: returns 1 when finished
Globals: hist_buff[],hist_size
Calls: unpackecommand()

Change log: 9/18/03 - Rich attempts to create, problem
	    getting command to copy to hist_buff
*******************************************************************************/

/*
int history(void)
{                                               	 *******************
	int count=0;                                     to be implemented later
	int choice;                                      *******************
	int size;            
	int i=0;

	printf("History\n\n");

	while(count<hist_size)
	{
		printf("%i:\t%s\n",hist_size+1,hist_buff[count]);
		count++;
	}

	printf("\n0:\tExit History\n\n==>");

	sys_req(READ,TERMINAL,choice,&size);

	if((choice>0)&&(choice<=hist_size))
	{
		unpackcommand(hist_buff[choice]);

		for (i=0;i<NUMCOMMANDS;i++)                        //check to see if command given
		{                                                  //is a valid command
			if (strcmp(name[i],commandline.name) == 0)
			break;
		}

		if (i<NUMCOMMANDS)
			(*fp[i]) ();                               //call procedure

		else
			printf("ERROR 01: Invalid command. Please enter a valid command.\n");

	}

	else
	{
		if(choice!=0)
		{
			printf("ERROR 14: Invalid choice. Exiting History");
		}
	}

	return 1;
}

*/

/*******************************************************************************
Procedure name: put_in_hist()
Purpose: To put commands in hist_buff for later use
Authors Names: Rich Paletta
Parameters: char command[]
Returns: returns 1 when finished
Globals: hist_size, hist_buff
Calls: N/A

Change log: 9/18/03 - Rich creates procedure
*******************************************************************************/
/*
put_in_hist(char command[])
{
	int pos;
	pos=0;

	while(pos<=strlen(command))
	{
		hist_buff[hist_size][pos]=command[pos];
		printf("%c\n",hist_buff[hist_size][pos]);
		pos++;
	}

	return 1;
}
*/
